import request from '@/utils/request'
import { 
  mockOrderList, 
  mockStatistics, 
  mockPaymentMethodData, 
  mockOrderStatusData, 
  mockTrendData 
} from './mock-payment'

// 查询微信订单数据库状态
export function getOrderStatus(outTradeNo) {
  return request({
    url: '/admin/payment/order/wechat/status',
    method: 'get',
    params: {
      outTradeNo: outTradeNo
    }
  })
}

// 查询微信订单列表
export function getWechatOrderList(query) {
  return request({
    url: '/admin/payment/wechat/order/list',
    method: 'post',
    params: query
  })
}

// 查询支付宝订单列表
export function getAlipayOrderList(query) {
  return request({
    url: '/admin/payment/alipay/order/list',
    method: 'post',
    params: query
  })
}

// 获取订单统计数据
export function getOrderStatistics(params) {
  return request({
    url: '/admin/payment/statistics/data',
    method: 'post',
    params: params
  })
}

// 微信订单退款
export function refundWechatOrder(data) {
  const params = new URLSearchParams()
  params.append('outTradeNo', data.outTradeNo)
  params.append('refundAmount', data.refundAmount.toString())
  params.append('reason', data.reason)

  return request({
    url: '/admin/payment/wechat/refund',
    method: 'post',
    data: params,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 创建支付订单
export function createPaymentOrder(data) {
  // 根据支付方式选择不同的接口
  if (data.paymentMethod === 'wechat') {
    // 微信支付接口 - 使用URLSearchParams
    const params = new URLSearchParams()
    params.append('description', data.title || data.description || '订单支付')
    params.append('totalAmount', data.amount.toString())
    params.append('payType', 'native')
    if (data.outTradeNo) {
      params.append('outTradeNo', data.outTradeNo)
    }
    if (data.openid) {
      params.append('openid', data.openid)
    }
    
    return request({
      url: '/admin/payment/h5/wx/pay',
      method: 'post',
      data: params,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })
  } else if (data.paymentMethod === 'alipay') {
    // 支付宝支付接口
    const params = new URLSearchParams()
    params.append('description', data.title || data.description || '订单支付')
    params.append('totalAmount', data.amount.toString())
    params.append('payType', 'native')
    if (data.outTradeNo) {
      params.append('outTradeNo', data.outTradeNo)
    }
    
    return request({
      url: '/admin/payment/h5/alipay/pay',
      method: 'post',
      data: params,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })
  }
}

// 查询订单列表
export function getOrderList(query) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      const { pageNum = 1, pageSize = 10 } = query
      const start = (pageNum - 1) * pageSize
      const end = start + pageSize
      const list = mockOrderList.slice(start, end)
      
      resolve({
        code: 200,
        msg: '查询成功',
        rows: list,
        total: mockOrderList.length
      })
    }, 500)
  })
  
  // 真实API调用
  // return request({
  //   url: '/payment/orders',
  //   method: 'get',
  //   params: query
  // })
}

// 获取订单详情
export function getOrderDetail(orderId) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      const order = mockOrderList.find(item => item.id == orderId)
      resolve({
        code: 200,
        msg: '查询成功',
        data: order || {}
      })
    }, 300)
  })
  
  // 真实API调用
  // return request({
  //   url: `/payment/orders/${orderId}`,
  //   method: 'get'
  // })
}

// 订单退款
export function refundOrder(data) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        msg: '退款申请成功',
        data: {
          refundId: 'REFUND_' + Date.now()
        }
      })
    }, 1000)
  })
  
  // 真实API调用
  // return request({
  //   url: '/payment/refund',
  //   method: 'post',
  //   data: data
  // })
}


// 获取支付方式统计
export function getPaymentMethodStats(query) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        msg: '查询成功',
        data: mockPaymentMethodData
      })
    }, 300)
  })
  
  // 真实API调用
  // return request({
  //   url: '/payment/method-stats',
  //   method: 'get',
  //   params: query
  // })
}

// 获取订单状态统计
export function getOrderStatusStats(query) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        msg: '查询成功',
        data: mockOrderStatusData
      })
    }, 300)
  })
  
  // 真实API调用
  // return request({
  //   url: '/payment/status-stats',
  //   method: 'get',
  //   params: query
  // })
}

// 获取订单趋势数据
export function getOrderTrends(query) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        msg: '查询成功',
        data: mockTrendData
      })
    }, 300)
  })
  
  // 真实API调用
  // return request({
  //   url: '/payment/trends',
  //   method: 'get',
  //   params: query
  // })
}
